package net.obviam.droidz;

import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;

public class MainThread extends Thread{
	//flag to hold game state
	private boolean running;
	private SurfaceHolder surfaceHolder;
	private MainGamePanel gamePanel;
	private static final String TAG = MainThread.class.getSimpleName();
	
	public MainThread(SurfaceHolder surfaceHolder, MainGamePanel gamePanel){
		super();
		this.surfaceHolder = surfaceHolder;
		this.gamePanel = gamePanel;
	}
	
	public void setRunning(boolean running){
		this.running = running;
	}
	@Override
	public void run() {
		Canvas canvas;
		long tickCount = 0L;
		Log.d(TAG, "Starting Game Loop");
		while(running){
			canvas = null;
			tickCount++;
			try{
				canvas = this.surfaceHolder.lockCanvas();
				synchronized (surfaceHolder) {
					this.gamePanel.onDraw(canvas);
				}
			//update game state
			//render state to the screen
			} finally {
				if(canvas != null){
					surfaceHolder.unlockCanvasAndPost(canvas);
				}
			}
		}
		Log.d(TAG, "Game loop executed " + tickCount + " times");
	}
}
